package com.atguigu.yygh.hosp.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.vo.order.SignInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class HospitalServiceImpl implements HospitalService {


    @Autowired
    private HospitalRepository hospitalRepository;

      @Autowired
     private    DictFeignClient dictFeignClient;

    //医院列表   条件查询分页
    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Pageable pageable= PageRequest.of(page-1,limit);
        //创建条件匹配器
        ExampleMatcher matcher=ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        Example<Hospital> example=Example.of(hospital,matcher);

        Page<Hospital> all = hospitalRepository.findAll(example, pageable);

        all.getContent().stream().forEach(item ->{
            this.setHospitalHosType(item);
        });
         return all;
    }

    private Hospital  setHospitalHosType(Hospital hospital){
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());


        hospital.getParam().put("hostypeString",hostypeString);
        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
        return hospital;
    }


    @Override
    public Hospital getByHosCode(String hoscode) {
        Hospital byHoscode = hospitalRepository.getHospitalByHoscode(hoscode);
        return  byHoscode;
    }

    @Override
    public void save(Map<String, Object> switchMap) {
        //判断是否存在相同数据  相同则修改  不存在添加
        String s = JSONObject.toJSONString(switchMap);
        Hospital hospital = JSONObject.parseObject(s, Hospital.class);


        String hoscode = hospital.getHoscode();
    Hospital hospitalExist=hospitalRepository.getHospitalByHoscode(hoscode);

    if(hospitalExist!=null){
        hospital.setStatus(hospitalExist.getStatus());
        hospital.setCreateTime(hospitalExist.getCreateTime());
        hospital.setUpdateTime(new Date());
        hospital.setIsDeleted(0);
        hospitalRepository.save(hospital);
    }else{
        hospital.setStatus(0);
        hospital.setCreateTime(new Date());
        hospital.setUpdateTime(new Date());
        hospital.setIsDeleted(0);
        hospitalRepository.save(hospital);
    }


    }
 //更新医院上线状态
    @Override
    public void updataStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);


    }


    //查询医院详情

    @Override
    public Map<String, Object> getHospById(String id) {
        Map<String,Object> result=new HashMap<>();
        //封装医院的信息 从cmn模块中查询到数据放入到map中
        Hospital hospital = this.setHospitalHosType(hospitalRepository.findById(id).get());
        result.put("hospital",hospital);
        result.put("bookingRule",hospital.getBookingRule());

        hospital.setBookingRule(null);

        return result;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospitalByHoscode = hospitalRepository.getHospitalByHoscode(hoscode);
        if(hospitalByHoscode!=null){
            return  hospitalByHoscode.getHosname();
        }
        return null;
    }

    //根据医院名称做模糊查询
    @Override
    public List<Hospital> findByHosname(String hosname) {
     return    hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    @Override
    public Map<String, Object> item(String hoscode) {
        HashMap<String, Object> result = new HashMap<>();

        //医院详情
        Hospital hospital = this.setHospitalHosType(this.getByHosCode(hoscode));
        result.put("hospital",hospital);

        //预约规则
        result.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        return result;
    }



}
